﻿using System;
using System.Collections.Generic;
using System.Text;
using Business.Entities;
using System.Data;
using System.Data.SqlClient;

namespace Data.Database
{
    public class ModuloAdapter:Adapter
    {
        public List<Modulo> GetAll()
        {
            List<Modulo> modulos = new List<Modulo>();
            try
            {
                this.OpenConnection();
                SqlCommand cmdModulos = new SqlCommand("select * from modulos", SqlConn);
                SqlDataReader drModulos = cmdModulos.ExecuteReader();


                while (drModulos.Read())
                {
                    Modulo mo = new Modulo();
                    mo.ID = (int)drModulos["id_modulo"];
                    mo.Descripcion = (string)drModulos["desc_modulo"];
                    modulos.Add(mo);

                }
                drModulos.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar lista de Modulos", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return modulos;
        }

        public Business.Entities.Modulo GetOne(int ID)
        {
            Modulo mo = new Modulo();
          

            try
            {
                this.OpenConnection();
                SqlCommand cmdModulos = new SqlCommand("select  * from modulos where id_modulo = @id", SqlConn);
                cmdModulos.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                SqlDataReader drModulos = cmdModulos.ExecuteReader();
                if (drModulos.Read())
                {
                    mo.ID = (int)drModulos["id_modulo"];
                    mo.Descripcion = (string)drModulos["desc_modulo"];

                }
                drModulos.Close();


            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar datos del Modulo", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return mo;
        }


        public void Delete(int ID)
        {

            try
            {
                this.OpenConnection();
                SqlCommand cmdDelete = new SqlCommand("delete from modulos where id_modulo=@id", SqlConn);
                cmdDelete.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                cmdDelete.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al eliminar modulo", Ex);
                throw ExcepcionManejada;

            }

            finally
            {
                this.CloseConnection();
            }


        }


        protected void Update(Modulo mo)
        {

            try
            {
                this.OpenConnection();
                SqlCommand cmdSave = new SqlCommand(
                "UPDATE modulos set desc_modulo=@desc_modulo WHERE id_modulo=@id", SqlConn);
                cmdSave.Parameters.Add("@id", SqlDbType.Int).Value = mo.ID;
                cmdSave.Parameters.Add("@desc_modulo", SqlDbType.VarChar, 50).Value = mo.Descripcion;
                cmdSave.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al modificar datos del modulo", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

        }


        protected void Insert(Modulo mo)
        {
            try
            {
                this.OpenConnection();
                SqlCommand cmdSave = new SqlCommand(
                "insert into modulos(desc_modulo) values(@desc_modulo) " +
                "select @@identity", SqlConn);

                cmdSave.Parameters.Add("@id", SqlDbType.Int).Value = mo.ID;
                cmdSave.Parameters.Add("@desc_modulo", SqlDbType.VarChar, 50).Value = mo.Descripcion;
                mo.ID = Decimal.ToInt32((decimal)cmdSave.ExecuteScalar());

            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al crear Modulo", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

        }





        public void Save(Modulo mo)
        {
            if (mo.State == BussinessEntity.States.Deleted)
            {
                this.Delete(mo.ID);
            }
            else if (mo.State == BussinessEntity.States.New)
            {
                this.Insert(mo);
            }
            else if (mo.State == BussinessEntity.States.Modified)
            {
                this.Update(mo);
            }
            mo.State = BussinessEntity.States.Unmodified;

        }


    }
}
